<?php
//推送景点给美团
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/meituan/MT.class.php';
$meituan = new Meituan();

$partnerId = $_REQUEST['partnerId'];
$signkey = $_REQUEST['signkey'];
$partnerDealId = current($_REQUEST['partnerDealId']);
$partnerPoiIds = explode("|",$partnerDealId);
$partnerPoiId  = $partnerPoiIds[1];

$PushPoi_YorN = $_REQUEST['push_land_f'];

$txt3 = json_encode($_REQUEST);
//是否推送景点信息
if($PushPoi_YorN == 1){
	file_get_contents("http://10.160.4.140/new/d/meituan/MT_PushPoi.php?partnerId=$partnerId&signkey=$signkey&partnerPoiId=$partnerPoiId");
}

$txt2= <<<H
{
  "partnerId": $partnerId,
  "signkey":"$signkey",
  "body": {
    "method": "page",
    "partnerDealId":
		$partnerDealId,
    "partnerPoiId": [
      "$partnerPoiId"
    ]
  }
}
H;

$arr = json_decode($txt2,true);
$partnerId  = $arr['partnerId'];  //第三方的ID
$method = $arr['body']['method']; //请求方式page:分页;multi:支持单个
$url = 'http://lvyou.meituan.com/rhone/lv/deal/push';
if($method == 'page'){ //分页查询
	$sel_total = "select count(id) as count from uu_qunar_use  where supplierIdentity='$partnerId'";
	$GLOBALS['le']->query($sel_total);
	$row=$GLOBALS['le']->fetch_assoc();
	$totalSize = $row['count'];	
	$partnerDealIds = $arr['body']['partnerDealId'];
	$get_product = $meituan->catch_product_information($partnerId,$partnerDealIds,$totalSize); 
	$signkey = explode("|",$arr['signkey']);
	$clientID = $signkey[0];
	$clientsecret = $signkey[1];
	
	date_default_timezone_set('GMT');
	$date = date('D, d M Y H:i:s e', time());
	date_default_timezone_set('PRC');
	$authorization = buildSign('POST',$date,$clientID,$clientsecret);

	$header = array(
		"Content-Type: application/json; charset=utf-8",
		"Date:" . $date,
		"PartnerId:" . $partnerId,
		"Authorization:" . $authorization,
	);	
	$result = mt_notice_post($get_product,$header,$url);
	$meituan->logfile($txt3.'|'.$result,"pushdeal");
}

function mt_notice_post($data,$header,$url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1); 
	curl_setopt($ch, CURLOPT_HEADER, 0); 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL,$url); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
	$result=  curl_exec($ch); 
	curl_close($ch);
	return $result;
}
function buildSign($method,$date,$clientID,$clientsecret){
	$string_to_sign = $method . ' ' . '/rhone/lv/deal/push' . "\n" . $date;
	$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $clientsecret, true));
	$authorization = 'MWS ' . $clientID . ':' . $signature;
	return $authorization;	
}

?>